const app = getApp();
const utils = require('../../utils/util.js');

Page({
  data: {
    auth: false,
    full_loading: true,
    user: {}
  },
  onLoad() {
    this.setData({
      statusBarHeight: app.my_config.statusBarHeight,
      topBarHeight: app.my_config.topBarHeight
    });
  },
  onShow() {
    this.userInfo(() => {
      this.setData({ full_loading: false });
    });
  },
  before_apply() {
    switch (this.data.user.auth) {
      case 0:
        wx.navigateTo({ url: '/pages/before-apply/before-apply' });
        break;
      case 1:
      case 3:
        wx.navigateTo({ url: '/pages/apply2/apply2?role=0' });
        break;
    }
  },
  userInfo(complete) {
    app.ajax('my/userinfo', null, (res) => {
      switch (res.sex) {
        case 0:
          res.sex_text = '未知';
          break;
        case 1:
          res.sex_text = '男';
          break;
        case 2:
          res.sex_text = '女';
          break;
      }

      app.avatar_format(res);

      this.setData({ user: res });
    }, null, () => {
      if (complete) {
        complete();
      }
    });
  },
  onPullDownRefresh() {
    wx.showLoading({ title: '刷新中' });
    this.userInfo(() => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    });
  },
  vip_date() {
    let date_text = utils.date_format('yyyy年MM月dd日到期', new Date(this.data.user.vip_time * 1000));
    app.modal('您的vip将于' + date_text);
  },
  openShanHaiWenHua() {
    wx.navigateToMiniProgram({
      appId: 'wx50f030c7f4a897ff'
    })
  },
  auth(e) {
    if (e.detail.userInfo) {
      wx.showLoading({
        title: '授权中',
        mask: true
      });

      app.userAuth(null, () => {
        wx.hideLoading();
        this.userInfo();
        app.set_user_data();
      });
    }
  }
});